package com.gosophia.taskManagement.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.opensymphony.xwork2.ActionContext;

/**
 * 保存新创建的任务
 * @author mayue
 *
 */
@Controller("SaveMissionAction")
@Scope("prototype")
public class SaveMissionAction extends BaseSupportAction{
    
    private static final long serialVersionUID = 1L;

    @Autowired
    private TaskManagementWebService taskManagementWebService;
    
    private MissionDTO missionDTO;

    @Override
    @LogRecord(eventAction="任务管理--任务发起",eventTarget="missionDTO")
    public String _execute() throws Exception {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserInfoDTO creatorDTO = userDetails.getUserAccountDTO().getUserInfoDTO();
        missionDTO.setCreatorDTO(creatorDTO);
        MissionDTO missionDTONew = taskManagementWebService.addMission(missionDTO);
        //不出异常，认为成功
        super.getRequestResult().setMessage("任务名称为  "+missionDTO.getMissionName()
                +", 任务编号为 "+ missionDTONew.getMissionCode()+" 的任务已发起成功");
        super.getRequestResult().setRsltCode("0");
        ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult());

        return SUCCESS;
    }

    public MissionDTO getMissionDTO() {
        return missionDTO;
    }

    public void setMissionDTO(MissionDTO missionDTO) {
        this.missionDTO = missionDTO;
    }
    
}
